package com.example.myapplication.view;

import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;

import com.example.myapplication.R;
import com.example.myapplication.databinding.ActivityMainBinding;
import com.example.myapplication.view.chart.ShowChartActivity;
import com.example.myapplication.view.toolbar.CoordinatorActivity;

public class MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {

    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;
    private PopupMenu mPopupMenu;
    private FragmentManager.FragmentLifecycleCallbacks mFragmentLifecycleCallbacks;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.toolbarRoot.rightFunc.setText("更多");
        binding.toolbarRoot.rightFunc.setVisibility(View.VISIBLE);

        binding.toolbarRoot.title.setText("周试图一");
        binding.toolbarRoot.back.setOnClickListener(v -> {
            Fragment primaryNavigationFragment = getSupportFragmentManager().getPrimaryNavigationFragment();
            if (primaryNavigationFragment != null) {
                if (!NavHostFragment.findNavController(primaryNavigationFragment).popBackStack())
                    finish();
            }
        });
        binding.toolbarRoot.rightFunc.setOnClickListener(v -> showMenu());

        binding.toolbarRoot.root.post(() -> {
            Fragment primaryNavigationFragment = getSupportFragmentManager().getPrimaryNavigationFragment();
            if (primaryNavigationFragment != null) {
                mFragmentLifecycleCallbacks = new FragmentManager.FragmentLifecycleCallbacks() {
                    @Override
                    public void onFragmentResumed(@NonNull FragmentManager fm, @NonNull Fragment f) {
                        super.onFragmentResumed(fm, f);
                        binding.toolbarRoot.rightFunc.setVisibility(f instanceof WeekOneFragment ?
                                View.VISIBLE : View.GONE);

                        FragmentName annotation = f.getClass().getAnnotation(FragmentName.class);
                        if (annotation != null) {
                            binding.toolbarRoot.title.setText(annotation.value());
                        }
                    }
                };
                primaryNavigationFragment.getChildFragmentManager().registerFragmentLifecycleCallbacks(mFragmentLifecycleCallbacks, true);
            }
        });
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        Fragment primaryNavigationFragment = getSupportFragmentManager().getPrimaryNavigationFragment();
        if (primaryNavigationFragment != null) {
            Fragment showingFragment = primaryNavigationFragment.getChildFragmentManager().getPrimaryNavigationFragment();
            if (showingFragment instanceof WeekOneFragment) {
                if (id == R.id.action_settings) {
                    Navigation.findNavController(this, R.id.nav_host_fragment_content_main)
                            .navigate(R.id.action_FirstFragment_to_CheckChartFragment);
                } else if (id == R.id.action_chart) {
                    Navigation.findNavController(this, R.id.nav_host_fragment_content_main)
                            .navigate(R.id.action_FirstFragment_to_ChartFragment);
                } else if (id == R.id.action_month_week) {
                    Navigation.findNavController(this, R.id.nav_host_fragment_content_main)
                            .navigate(R.id.action_FirstFragment_to_MonthWeekFragment);
                } else if (id == R.id.action_show_chart) {
                    ShowChartActivity.start(this);
                } else if (id == R.id.action_coordinator_toolbar) {
                    CoordinatorActivity.start(this);
                }
            }
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Fragment primaryNavigationFragment = getSupportFragmentManager().getPrimaryNavigationFragment();
        if (primaryNavigationFragment != null) {
            primaryNavigationFragment.getChildFragmentManager().unregisterFragmentLifecycleCallbacks(mFragmentLifecycleCallbacks);
        }
    }

    private void showMenu() {
        if (mPopupMenu == null) {
            //创建弹出式菜单对象
            mPopupMenu = new PopupMenu(this, binding.toolbarRoot.rightFunc);//第二个参数是绑定的那个view
            //获取菜单填充器
            MenuInflater inflater = mPopupMenu.getMenuInflater();
            //填充菜单
            inflater.inflate(R.menu.menu_main, mPopupMenu.getMenu());
            //绑定菜单项的点击事件
            mPopupMenu.setOnMenuItemClickListener(this);
        }
        //显示
        mPopupMenu.show();
    }
}